package net.oschina.app.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import net.oschina.app.R;

/**
 * Created by huang on 2017-4-7.
 */

public class EventsWebViewActivity extends AppCompatActivity{

    private WebView mWebView;
    private Toolbar mToolBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    //初始化视图
    private void initView() {
        setContentView(R.layout.activity_events_webview);
        mToolBar = (Toolbar) findViewById(R.id.tb_events_webview);
        setSupportActionBar(mToolBar);
        mToolBar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        mWebView = (WebView) findViewById(R.id.wv_events_webview);
    }

    //初始化数据
    private void initData() {
        String url = getIntent().getStringExtra("url");
        //展示webview
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient(){
            //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);// 设置允许js弹出alert
        settings.setJavaScriptEnabled(true);//设置可以加载JS
        Log.d("EventsWebViewActivity", url);
    }

    //初始化事件
    private void initEvent() {
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
